package cc.cc4414.spring.generator.mybatis;

import cc.cc4414.spring.mybatis.entity.BaseEntity;
import cc.cc4414.spring.mybatis.service.ICcService;
import cc.cc4414.spring.mybatis.service.impl.CcServiceImpl;
import com.baomidou.mybatisplus.core.enums.SqlLike;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.LikeTable;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import lombok.experimental.UtilityClass;

import java.util.ArrayList;
import java.util.List;

/**
 * 代码生成器
 *
 * @author cc 2021年04月20日
 */
@UtilityClass
public class CodeGenerator {
    /**
     * 生成mybatis plus相关代码
     *
     * @param generateConfig 配置
     */
    public void generateMp(GenerateConfig generateConfig) {
        String moduleName = generateConfig.getModuleName();
        String parent = generateConfig.getParent();
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl(generateConfig.getUrl());
        dsc.setDriverName(generateConfig.getDriverName());
        dsc.setUsername(generateConfig.getUsername());
        dsc.setPassword(generateConfig.getPassword());
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName(moduleName);
        pc.setParent(parent);
        mpg.setPackageInfo(pc);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        strategy.setLikeTable(new LikeTable(moduleName + "_", SqlLike.RIGHT));
        strategy.setTablePrefix(moduleName + "_");
        strategy.setChainModel(true);
        if (generateConfig.isExtendsBaseEntity()) {
            strategy.setSuperEntityClass(BaseEntity.class);
            strategy.setSuperEntityColumns(BaseEntity.ID, BaseEntity.NAME, BaseEntity.DEPT_ID,
                    BaseEntity.DEPT_NAME, BaseEntity.DELETED, BaseEntity.DISABLED,
                    BaseEntity.GMT_CREATE, BaseEntity.CREATOR_ID, BaseEntity.CREATOR_NAME,
                    BaseEntity.GMT_MODIFIED, BaseEntity.MODIFIER_ID, BaseEntity.MODIFIER_NAME);
            strategy.setSuperServiceClass(ICcService.class);
            strategy.setSuperServiceImplClass(CcServiceImpl.class);
        }
        mpg.setStrategy(strategy);

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = generateConfig.getOutputDir();
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setAuthor(generateConfig.getAuthor());
        gc.setSwagger2(true);
        gc.setOpen(false);
        mpg.setGlobalConfig(gc);

        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
            }
        };
        List<FileOutConfig> focList = new ArrayList<>();
        focList.add(new FileOutConfig("/cc/cc4414/templates/mapper.xml.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输入文件名称
                return projectPath + "/src/main/resources/mapper/" + pc.getModuleName()
                        + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            }
        });
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);

        TemplateConfig templateConfig = new TemplateConfig();
        templateConfig.setEntity("/cc/cc4414/templates/entity.java");
        templateConfig.setMapper("/cc/cc4414/templates/mapper.java");
        templateConfig.setService("/cc/cc4414/templates/service.java");
        templateConfig.setServiceImpl("/cc/cc4414/templates/serviceImpl.java");
        templateConfig.setController("/cc/cc4414/templates/controller.java");
        templateConfig.setXml(null);
        mpg.setTemplate(templateConfig);

        // 切换为 freemarker 模板引擎
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }
}
